﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DocManagerORM.ORM;

namespace DocManagerControls.Controls
{
    /// <summary>
    /// Logica di interazione per FilterDataControl.xaml
    /// </summary>
    public partial class FilterDataControl : UserControl
    {
        IDictionary <string,ComboBox> _comboDict = new Dictionary <string,ComboBox>() ;
        IList<string> _esclusi = new List<string>() { "Documento", "Documenti", "Iters" };


        public FilterDataControl()
        {
            InitializeComponent();
        }

        public ItemsControl DysplayerControl
        {
            get; set;
        }

        public void CreaComboFiltri(IList<string> pListTabelle)
        {
            _comboDict.Clear(); 
            myStackFiltri.Children.Clear();

            StackPanel stack;
            ComboBox combo = null;
            Label label = null;
            foreach (var tabella in pListTabelle)
            {
                if (_esclusi.Contains(tabella)) continue;
                //var dati = DocManagerORM.Caching.CacheBase.getDati(tabella);
                label = new Label() {Content = tabella +" :"  };
                stack = new StackPanel() { Orientation = System.Windows.Controls.Orientation.Vertical }; 
                combo = new ComboBox() { Width = 120, Margin = new Thickness(2),Name = "cmb_"+ tabella,Tag= tabella} ; 
                
                if(tabella.Equals("categorie", StringComparison.CurrentCultureIgnoreCase ))
                    combo.SelectionChanged += new SelectionChangedEventHandler(comboCat_SelectionChanged); 
                
                DocManagerORM.Caching.CacheBase.BindaComboBoxdataPredefinito(ref combo, tabella);

                stack.Children.Add(label);
                stack.Children.Add(combo);
                myStackFiltri.Children.Add(stack);
                _comboDict.Add( "cmb_"+ tabella , combo); 

            }
        }


        void comboCat_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var id = ((ComboBox)sender).SelectedValue;
            if( id == null) return ;

            int idVal = 0;
            int.TryParse(id.ToString(), out idVal);

            if (idVal < 1) return;

            var combo = _comboDict["cmb_SottoCategorie"];
            if (combo == null) return;

            DocManagerORM.Caching.CacheBase.BindaComboBoxdata<SottoCategorie>(ref combo, combo.Tag.ToString(),"ID",
                "Categoria", idVal);

        }//fM





    }//fC
}
